package com.xieke.admin.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xieke.admin.dto.UserInfo;
import com.xieke.admin.entity.User;
import com.xieke.admin.mapper.UserMapper;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


import com.xieke.admin.mapper.PicMapper;
import com.xieke.admin.entity.Pic;
import com.xieke.admin.service.IPicService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import java.io.*;
import java.util.List;


@Service
public class PicServiceImpl extends ServiceImpl<PicMapper, Pic> implements IPicService {

    @Resource
    private PicMapper picMapper;
    @Resource
    private UserMapper userMapper;
    @Value("${my.upload.imgPath}")
    private String imgPath;

    @Override
    @Transactional
    public int savePic(MultipartFile file,Pic pic) {

        int count = 0;
//        String fileName = file.getOriginalFilename();
//        String suffixName = fileName.substring(fileName.lastIndexOf("."));
//        UserInfo userInfo = (UserInfo) SecurityUtils.getSubject().getPrincipal();
//        String destFileName = System.getProperty("user.dir") + "/src/main/resources/static/upload";// + userInfo.getId();
//        System.out.println(destFileName);
//        File dest = new File(destFileName);
//        if (!dest.getParentFile().exists()) {
//            dest.getParentFile().mkdirs();
//        }
//        try {
//            FileOutputStream os = new FileOutputStream(destFileName+"\\"+userInfo.getId()+suffixName);
//            InputStream in = file.getInputStream();
//            int b = 0;
//            while((b=in.read())!=-1){
//                os.write(b);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        //上传文件时可能会带普通的参数，这样可以直接拿到
        //可能会有多个文件同时上传
        BufferedOutputStream stream = null;
            if (file.isEmpty()){
            }
        String fileName = file.getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        UserInfo userInfo = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            try {
                byte[] b = file.getBytes();
                // String path = "src/main/resources/uploadFile/";
                String path = imgPath+userInfo.getId()+suffixName;
//                String path = "/Users/halo/Documents/HtmlProject/"+fileName;
                stream = new BufferedOutputStream(new FileOutputStream(new File(path)));
                stream.write(b);
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        pic.setCid(userInfo.getId());
        pic.setUrl("/static/upload/"+userInfo.getId()+suffixName);
        if(pic.getId()==null){
            count = picMapper.insert(pic);
            User user = userMapper.selectById(userInfo.getId());
            user.setpId(pic.getId());
            userMapper.updateById(user);
        }

        return count;
    }
}